package zhongbo.java.air.ingestion.listener;

import cn.hutool.core.convert.ConvertException;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.ingestion.dto.FlightPathDto;

import java.util.Date;

/**
 * @author WeiJin
 * @version 1.0
 * @since 2025/4/3 10:13
 */
@Component
@Slf4j
public class RedisFlightPathListener implements MessageListener {

    @Autowired
    private RedisFlightPathPublisher redisFlightPathPublisher;


    @Override
    public void onMessage(Message message, byte[] bytes) {
        String msg = new String(message.getBody());
        // 打印日志4
        // System.out.println("msg:" + msg);
        log.info("收到消息：{}", msg);
        // 类型转换
        FlightPathDto flightPathDto = convert(msg);
        System.out.println("fli:" + flightPathDto);
        // 转换为JSON字符串
        String jsonStr = JSONUtil.toJsonStr(flightPathDto);
        // 发送信息
        redisFlightPathPublisher.publish(jsonStr);


    }

    public FlightPathDto convert(String message) {
        // System.out.println(message);
        // 转换为JSON对象
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONUtil.parseObj(message);
            // System.out.println("jsonObject:"+jsonObject);
        } catch (Exception e) {
            log.error("JSON对象转换失败");
        }
        // 转换为Dto
        FlightPathDto flightPathDto = new FlightPathDto();
        try {
            flightPathDto.setIcaoCode(jsonObject.getStr("icao"));
        } catch (Exception e) {
            log.error("icao获取失败");
        }

        JSONArray trace = null;
        try {
            // 获取trace
            trace = jsonObject.getJSONArray("trace");

        } catch (Exception e) {
            log.error("trace获取失败");
        }

        try {
            // 获取timestamp
            if (trace != null) {
                double timestamp;
                String strTimestamp = jsonObject.getStr("timestamp");
                // 如果字符串类型的timestamp长度等于13，就不需要乘以1000
                if (strTimestamp.length() == 13) {
                    timestamp = Double.parseDouble(strTimestamp) + trace.get(0, Double.class);
                } else {
                    timestamp = Double.parseDouble(strTimestamp) * 1000 + trace.get(0, Double.class);
                }
                flightPathDto.setTimestamp(new Date((long) timestamp));
            }

        } catch (Exception e) {
            log.error("获取timestamp失败");
        }
        // 获取纬度
        try {
            if (trace != null) {
                flightPathDto.setLatitude(trace.get(1, Double.class));
            }
        } catch (ConvertException e) {
            log.error("latitude获取失败");
        }
        // 获取经度
        try {
            if (trace != null) {
                flightPathDto.setLongitude(trace.get(2, Double.class));
            }

        } catch (ConvertException e) {
            log.error("longitude获取失败");
        }
        // 获取海拔高度
        try {
            if (trace != null) {

                String strAltitude = trace.get(3, String.class);
                if ("ground".equals(strAltitude)) {
                    flightPathDto.setAltitude(0.0);
                } else {
                    flightPathDto.setAltitude(Double.parseDouble(strAltitude));
                }
            }


        } catch (ConvertException e) {
            log.error("altitude获取失败");
        }
        // 获取地速
        try {
            if (trace != null) {
                flightPathDto.setSpeed(trace.get(4, Double.class));
            }

        } catch (ConvertException e) {
            log.error("speed获取失败");
        }
        // 获取方向角
        try {
            if (trace != null) {
                flightPathDto.setHeading(trace.get(5, Double.class));
            }

        } catch (ConvertException e) {
            log.error("heading获取失败");
        }
        // 获取source_type
        try {
            if (trace != null) {
                JSONObject object = trace.get(8, JSONObject.class);
                // 做一个非空判断
                if (object != null) {
                    flightPathDto.setSourceType(object.get("type", String.class));
                }
            }

        } catch (ConvertException e) {
            log.error("sourceType获取失败");
        }
        return flightPathDto;
    }
}
